package org.zhengquan.service;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhengquan.common.CacheKeyConstants;
import org.zhengquan.common.RequestHolder;
import org.zhengquan.dao.SysAclMapper;
import org.zhengquan.dao.SysRoleAclMapper;
import org.zhengquan.dao.SysRoleUserMapper;
import org.zhengquan.pojo.SysAcl;
import org.zhengquan.pojo.SysUser;
import org.zhengquan.util.JsonMapper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhengquan
 * @date 2017/11/5
 */
@Service
public class SysCoreService {

    @Autowired
    private SysAclMapper sysAclMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysCacheService sysCacheService;

    public List<SysAcl> selectCurUserAclList() {
        int curUserId = RequestHolder.getCurrentUser().getId();
        return selectUserAclList(curUserId);
    }

    public List<SysAcl> selectRoleAclList(int roleId) {
        List<Integer> aclIdList = sysRoleAclMapper.selectAclIdListByRoleIdList(Lists.<Integer>newArrayList(roleId));
        if (CollectionUtils.isEmpty(aclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.selectByIdList(aclIdList);
    }

    public List<SysAcl> selectUserAclList(int userId) {
        if (isSuperAdmin()) {
            sysAclMapper.selectAllAcl();
        }
        List<Integer> userRoleIdList = sysRoleUserMapper.selectRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(userRoleIdList)) {
            return Lists.newArrayList();
        }
        List<Integer> userAclIdList = sysRoleAclMapper.selectAclIdListByRoleIdList(userRoleIdList);
        if (CollectionUtils.isEmpty(userAclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.selectByIdList(userAclIdList);
    }

    public boolean isSuperAdmin() {
        SysUser sysUser = RequestHolder.getCurrentUser();
        if (sysUser.getMail().contains("admin")) {
            return true;
        }
        return false;
    }

    public boolean hasUrlAcl(String url) {
        if (isSuperAdmin()) {
            return true;
        }
        List<SysAcl> aclList = sysAclMapper.selectByUrl(url);
        if (CollectionUtils.isEmpty(aclList)) {
            return true;
        }
        List<SysAcl> curUserAclList = selectCurUserAclList();
        Set<Integer> curUserAclSet = curUserAclList.stream().map(sysAcl -> sysAcl.getId()).collect(Collectors.toSet());
        boolean hasValidAcl = false;
        //规则：只要有一个权限点有权限,就认为有权限
        for (SysAcl sysAcl : aclList) {
            if (sysAcl == null || sysAcl.getState() != 1) {
                continue;
            }
            hasValidAcl = true;
            if (curUserAclSet.contains(sysAcl.getId())) {
                return true;
            }
        }
        if (!hasValidAcl) {
            return true;
        }
        return false;
    }

    public List<SysAcl> selectCurUserAclListFromCache() {
        int userId = RequestHolder.getCurrentUser().getId();
        String cahceValue = sysCacheService.getFromCache(CacheKeyConstants.USER_ACLS, String.valueOf(userId));
        if (StringUtils.isBlank(cahceValue)) {
            List<SysAcl> aclList = selectCurUserAclList();
            if(CollectionUtils.isEmpty(aclList)){
                sysCacheService.saveCache(JsonMapper.obj2Str(aclList),600,CacheKeyConstants.USER_ACLS,String.valueOf(userId));
            }
            return aclList;
        }
        return JsonMapper.str2Obj(cahceValue, new TypeReference<List<SysAcl>>() {
        });

    }
}
